﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace YouTubeStormer
{
    class CommentPosting
    {
        Communications communications;

        public CommentPosting(Communications communications)
        {
            this.communications = communications;
        }

        public enum Result
        {
            Commented,
            Errored
        }

        public Result PerformComment(out Main.DebugDataHelper DebugData, out string CommentID, string commentText, string videoID)
        {
            DebugData = new Main.DebugDataHelper();

            string ResponseData = communications.PerformGet(Communications.YoutubeWatchUrl + videoID);
            string sessionToken = Regex.Split(Regex.Split(ResponseData, "name=\"session_token\" value=\"")[1], "\"/>")[0];

            var postData = new Communications.PostData();
            postData.AddValue("form_id", "");
            postData.AddValue("reply_parent_id", "");
            postData.AddValue("video_id", videoID);
            postData.AddValue("return_ajax", "true");
            postData.AddValue("session_token", sessionToken);
            postData.AddValue("comment", commentText);
            DebugData.Input = postData.ToString();

            ResponseData = communications.PerformPost(Communications.YoutubeCommentUrl, postData, referer: Communications.YoutubeWatchUrl + videoID);
            DebugData.Output = ResponseData;

            if (ResponseData.Contains("data-id"))
            {
                CommentID = CommentDetails.ParseComments(ResponseData)[0].ID;
                Properties.Settings.Default.CommentLog.Add(CommentID);
                Properties.Settings.Default.Save();
                return Result.Commented;
            }
            else
            {
                CommentID = "";
                return Result.Errored;
            }
        }

        public CommentDetails.CommentDetail FindCommentOnPage(string videoID)
        {
            var comment = CommentDetails.FindComment(CommentDetails.ParseComments(communications.PerformGet(Communications.YoutubeWatchUrl + videoID)));
            if (comment == null)
                comment = CommentDetails.FindComment(CommentDetails.ParseComments(communications.PerformGet(Communications.YoutubeCommentServerUrl + videoID)));

            return comment;
        }

        public static string GenerateComment()
        {
            var random = new Random();

            for (int i = 0; i < random.Next(0, 40); i++)
            {
                random.Next(0, random.Next(0, i));
            }

            string comment = "";
            var p1 = Properties.Settings.Default.CommentsP1;
            comment += p1[random.Next(0, p1.Count)];
            var p2 = Properties.Settings.Default.CommentsP2;
            comment += p2[random.Next(0, p2.Count)];
            var p3 = Properties.Settings.Default.CommentsP3;
            comment += p3[random.Next(0, p3.Count)];
            var p4 = Properties.Settings.Default.CommentsP4;
            comment += p4[random.Next(0, p4.Count)];
            var p5 = Properties.Settings.Default.CommentsP5;
            comment += p5[random.Next(0, p5.Count)];
            var p6 = Properties.Settings.Default.CommentsP6;
            comment += p6[random.Next(0, p6.Count)];
            var p7 = Properties.Settings.Default.Links;
            return comment.Replace("{LINK}", p7[random.Next(0, p7.Count)]);
        }

    }
}
